package com.zmj.framework.base.sql;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zmj.framework.exception.business.BizException;
import com.zmj.framework.constant.StringPool;
import com.zmj.framework.util.StringUtil;

import java.util.Map;

/**
 * @author scy
 * 条件拼接
 */
public class SqlKeyword {
	private static final String SQL_REGEX = "'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";

	private static final String EQUAL = "_equal";
	private static final String NOT_EQUAL = "_notequal";
	private static final String LIKE = "_like";
	private static final String NOT_LIKE = "_notlike";
	private static final String GT = "_gt";
	private static final String GE = "_ge";
	private static final String LT = "_lt";
	private static final String LE = "_le";
	private static final String DATE_GT = "_dategt";
	private static final String DATE_EQUAL = "_dateequal";
	private static final String DATE_LT = "_datelt";
	private static final String IS_NULL = "_null";
	private static final String NOT_NULL = "_notnull";
	private static final String IGNORE = "_ignore";
	private static final String SELECT = "SELECT";

	/**
	 * 条件构造器
	 *
	 * @param query 查询字段
	 * @param qw    查询包装类
	 */
	public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw,String alias) {
		if (ObjectUtil.isEmpty(query)) {
			return;
		}
		query.forEach((k, v) -> {
			if (ObjectUtil.hasEmpty(k, v) || k.endsWith(IGNORE)) {
				return;
			}
			if(k.toUpperCase().contains(SELECT) || String.valueOf(v).toUpperCase().contains(SELECT)) {
				throw new BizException("查询参数错误，不允许包含select语句");
			}
			if (k.endsWith(EQUAL)) {
				qw.eq(getColumnCommand(k, EQUAL,alias), v);
			} else if (k.endsWith(NOT_EQUAL)) {
				qw.ne(getColumnCommand(k, NOT_EQUAL,alias), v);
			} else if (k.endsWith(NOT_LIKE)) {
				qw.notLike(getColumnCommand(k, NOT_LIKE,alias), v);
			} else if (k.endsWith(GT)) {
				qw.gt(getColumnCommand(k, GT,alias), v);
			} else if (k.endsWith(GE)) {
				qw.ge(getColumnCommand(k, GE,alias), v);
			} else if (k.endsWith(LT)) {
				qw.lt(getColumnCommand(k, LT,alias), v);
			} else if (k.endsWith(LE)) {
				qw.le(getColumnCommand(k, LE,alias), v);
			} else if (k.endsWith(DATE_GT)) {
				qw.gt(getColumnCommand(k, DATE_GT,alias), v);
			} else if (k.endsWith(DATE_EQUAL)) {
				qw.eq(getColumnCommand(k, DATE_EQUAL,alias), v);
			} else if (k.endsWith(DATE_LT)) {
				qw.lt(getColumnCommand(k, DATE_LT,alias), v);
			} else if (k.endsWith(IS_NULL)) {
				qw.isNull(getColumnCommand(k, IS_NULL,alias));
			} else if (k.endsWith(NOT_NULL)) {
				qw.isNotNull(getColumnCommand(k, NOT_NULL,alias));
			} else {
				qw.like(getColumnCommand(k, LIKE,alias), v);
			}
		});
	}

	/**
	 * 获取字段分发
	 * @param column
	 * @param keyword
	 * @param alias
	 * @return
	 */
	public static String getColumnCommand(String column, String keyword,String alias) {
		if(StrUtil.isNotBlank(alias)) {
			return getColumnUseAlias(column,keyword,alias);
		}
		return getColumn(column,keyword);
	}
	/**
	 * 获取数据库字段
	 *
	 * @param column  字段名
	 * @param keyword 关键字
	 * @return
	 */
	private static String getColumn(String column, String keyword) {
		return StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword));
	}

	/**
	 * 获取数据库字段 并指定别名
	 * @param column  字段名
	 * @param keyword 关键字
	 * @param alias   别名
	 * @return
	 */
	private static String getColumnUseAlias(String column,String keyword,String alias) {
		return alias+"."+StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword));
	}
	/**
	 * 把SQL关键字替换为空字符串
	 *
	 * @param param 关键字
	 * @return string
	 */
	public static String filter(String param) {
		if (param == null) {
			return null;
		}
		return param.replaceAll("(?i)" + SQL_REGEX, StringPool.EMPTY);
	}

}
